package com.qinyu.treehole.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinyu.treehole.bo.FeedBo;
import com.qinyu.treehole.entities.Feed;
import com.qinyu.treehole.entities.Forward;
import com.qinyu.treehole.entities.SimpleFeed;
import com.qinyu.treehole.entities.User;
import com.qinyu.treehole.mapper.FeedMapper;
import com.qinyu.treehole.service.CommentService;
import com.qinyu.treehole.service.FeedService;
import com.qinyu.treehole.service.ForwardService;
import com.qinyu.treehole.service.UserService;
import com.qinyu.treehole.tool.FeedTool;
import com.qinyu.treehole.tool.UserTool;
import com.qinyu.treehole.vo.FeedVo;
import com.qinyu.treehole.vo.ForwardFeedVo;
import com.qinyu.treehole.vo.LoginFeedVo;
import com.qinyu.treehole.vo.LoginForwardFeedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class FeedServiceImpl extends ServiceImpl<FeedMapper, Feed> implements FeedService {

    @Autowired
    private RedisTemplate jsonRedisTemplate;
    @Autowired
    private FeedMapper feedMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ForwardService forwardService;
    @Autowired
    private CommentService commentService;

    @Override
    public long getUserFeedCountByUserId(Integer userId) {
        return feedMapper.getUserFeedCountByUserId(userId);
    }

    @Override
    public long getUserFeedCountBySelfUserId(Integer userId) {
        return feedMapper.getUserFeedCountBySelfUserId(userId);
    }


    @Override
    public List<ForwardFeedVo> getForwardFeedVoByUserId(Integer userId) {
        List<SimpleFeed> simpleFeeds = getForwardAndFeedByUserId(userId);
        return parseFeedToForwardFeedVo(simpleFeeds);
    }

    @Override
    public List<LoginForwardFeedVo> getLoginForwardFeedVoByUserId(Integer userId, Integer uid) {
        List<SimpleFeed> simpleFeeds = getForwardAndFeedByUserId(uid);
        return parseFeedToLoginForwardFeedVo(simpleFeeds, userId);
    }

    @Override
    public List<LoginForwardFeedVo> getLoginForwardFeedVoBySelfUserId(Integer userId) {
        List<SimpleFeed> simpleFeeds = getForwardAndFeedBySelftUserId(userId);
        return parseFeedToLoginForwardFeedVo(simpleFeeds, userId);
    }

    @Override
    public List<ForwardFeedVo> parseFeedToForwardFeedVo(List<SimpleFeed> feeds) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();

        return feeds.stream().map(item->{
            ForwardFeedVo vo = new ForwardFeedVo();;

            User user = userService.getById(item.getUserId());
            if(user == null) return  null;
            Feed feed = getById(item.getFeedId());

            // 0表示匿名，需要隐藏信息
            if(feed.getStatus() == 0){
                vo.setUname(UserTool.USER_INVISIBLE_NAME);
                vo.setAvatar(UserTool.USER_INVISIBLE_AVATAR);
            }else{
                vo.setUserId(user.getId());
                vo.setUname(user.getName());
                vo.setAvatar(user.getAvatar());
            }

            vo.setFeedId(feed.getId());
            vo.setContent(feed.getContent());
            vo.setTime(feed.getCreateTime());

            // 设置点赞数
            Long likeCount = ops.size(FeedTool.FEED_NAME + feed.getId());
            vo.setLikeCount(likeCount);

            // 设置转发数
            Long forwardCount = forwardService.getForwardCountByFeedId(feed.getId());
            vo.setForwardCount(forwardCount);

            // 设置评论数
            long commentCount = commentService.getCommentCountByFeedId(feed.getId());
            vo.setCommentCount(commentCount);

            if(item.getForwardId() != null){
                Forward forward = forwardService.getById(item.getForwardId());

                vo.setForwardId(forward.getId());

                User forwardUser = userService.getById(forward.getUserId());
                if(forwardUser == null) return null;
                vo.setForwardUserId(forwardUser.getId());
                vo.setForwardUserName(forwardUser.getName());
                vo.setForwardAvatar(forwardUser.getAvatar());
                vo.setForwardTime(forward.getCreateTime());
            }

            return vo;
        }).filter(obj->obj != null).collect(Collectors.toList());
    }

    @Override
    public List<LoginForwardFeedVo> parseFeedToLoginForwardFeedVo(List<SimpleFeed> feeds, Integer userId) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();

        return feeds.stream().map(item->{
            LoginForwardFeedVo vo = new LoginForwardFeedVo();;

            User user = userService.getById(item.getUserId());
            if(user == null) return null;

            Feed feed = getById(item.getFeedId());

            // 0表示匿名，需要隐藏信息
            if(feed.getStatus() == 0){
                vo.setUname(UserTool.USER_INVISIBLE_NAME);
                vo.setAvatar(UserTool.USER_INVISIBLE_AVATAR);
            }else{
                vo.setUserId(user.getId());
                vo.setUname(user.getName());
                vo.setAvatar(user.getAvatar());
            }

            vo.setFeedId(feed.getId());
            vo.setContent(feed.getContent());
            vo.setTime(feed.getCreateTime());

            // 设置点赞数
            Long likeCount = ops.size(FeedTool.FEED_NAME + feed.getId());
            vo.setLikeCount(likeCount);

            // 判断是否点赞
            Boolean isLike = ops.isMember(FeedTool.FEED_NAME + feed.getId(), userId);
            vo.setIsLike(isLike);

            // 设置转发数
            Long forwardCount = forwardService.getForwardCountByFeedId(feed.getId());
            vo.setForwardCount(forwardCount);

            // 判断是否转发
            boolean isForward = forwardService.isForwardFeedByUserId(feed.getId(), userId);
            vo.setIsForward(isForward);

            // 设置评论数
            long commentCount = commentService.getCommentCountByFeedId(feed.getId());
            vo.setCommentCount(commentCount);

            // 判断是否评论
            boolean isComment = commentService.isCommentFeedByUserId(feed.getId(), userId);
            vo.setIsComment(isComment);

            if(item.getForwardId() != null){
                Forward forward = forwardService.getById(item.getForwardId());

                vo.setForwardId(forward.getId());

                User forwardUser = userService.getById(forward.getUserId());
                if(forwardUser == null) return null;
                vo.setForwardUserId(forwardUser.getId());
                vo.setForwardUserName(forwardUser.getName());
                vo.setForwardAvatar(forwardUser.getAvatar());
                vo.setForwardTime(forward.getCreateTime());
            }

            return vo;
        }).filter(obj->obj != null).collect(Collectors.toList());
    }

    @Override
    public boolean saveFeed(FeedBo feedBo) {
        Feed feed = new Feed();
        feed.setUserId(feedBo.getUserId());
        feed.setStatus(feedBo.getStatus());
        feed.setContent(feedBo.getContent());

        return save(feed);
    }

    @Override
    public void addLike(Integer userId, Integer feedId) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();
        ops.add(FeedTool.FEED_NAME+feedId, userId);
    }

    @Override
    public void removeLike(Integer userId, Integer feedId) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();
        ops.remove(FeedTool.FEED_NAME+feedId, userId);
    }

    @Override
    public List<SimpleFeed> getForwardAndFeed() {
        return feedMapper.getForwardAndFeed();
    }

    public List<SimpleFeed> getForwardAndFeedByUserId(Integer userId){
        return feedMapper.getForwardAndFeedByUserId(userId);
    }

    @Override
    public List<SimpleFeed> getForwardAndFeedByUserIdFollow(Integer userId) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();
        Set<Integer> set = ops.members(UserTool.FOLLOW_NAME + userId);
        if(set.size() == 0) set.add(null);
        return feedMapper.getForwardAndFeedByUserSet(set);
    }

    @Override
    public List<FeedVo> searchAllFeedByContent(String content) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();

        return feedMapper.searchFeedByContent(content).stream().map(feedId->{
            FeedVo vo = new FeedVo();

            Feed feed = getById(feedId);
            User user = userService.getById(feed.getUserId());
            if(user == null) return null;

            // 0表示匿名，需要隐藏信息
            if(feed.getStatus() == 0){
                vo.setUname(UserTool.USER_INVISIBLE_NAME);
                vo.setAvatar(UserTool.USER_INVISIBLE_AVATAR);
            }else{
                vo.setUserId(user.getId());
                vo.setUname(user.getName());
                vo.setAvatar(user.getAvatar());
            }

            vo.setFeedId(feedId);
            vo.setContent(feed.getContent());
            vo.setTime(feed.getCreateTime());

            // 设置点赞数
            Long likeCount = ops.size(FeedTool.FEED_NAME + feed.getId());
            vo.setLikeCount(likeCount);

            // 设置转发数
            Long forwardCount = forwardService.getForwardCountByFeedId(feed.getId());
            vo.setForwardCount(forwardCount);

            // 设置评论数
            long commentCount = commentService.getCommentCountByFeedId(feed.getId());
            vo.setCommentCount(commentCount);

            return vo;
        }).filter(obj->obj != null).collect(Collectors.toList());
    }

    @Override
    public List<LoginFeedVo> searchAllFeedLoginByContent(String content, Integer userId) {
        SetOperations<String, Integer> ops = jsonRedisTemplate.opsForSet();

        return feedMapper.searchFeedByContent(content).stream().map(feedId->{
            LoginFeedVo vo = new LoginFeedVo();;

            Feed feed = getById(feedId);
            User user = userService.getById(feed.getUserId());
            if(user == null) return null;
            // 0表示匿名，需要隐藏信息
            if(feed.getStatus() == 0){
                vo.setUname(UserTool.USER_INVISIBLE_NAME);
                vo.setAvatar(UserTool.USER_INVISIBLE_AVATAR);
            }else{
                vo.setUserId(user.getId());
                vo.setUname(user.getName());
                vo.setAvatar(user.getAvatar());
            }

            vo.setFeedId(feedId);
            vo.setContent(feed.getContent());
            vo.setTime(feed.getCreateTime());

            // 设置点赞数
            Long likeCount = ops.size(FeedTool.FEED_NAME + feed.getId());
            vo.setLikeCount(likeCount);

            // 判断是否点赞
            Boolean isLike = ops.isMember(FeedTool.FEED_NAME + feed.getId(), userId);
            vo.setIsLike(isLike);

            // 设置转发数
            Long forwardCount = forwardService.getForwardCountByFeedId(feed.getId());
            vo.setForwardCount(forwardCount);

            // 判断是否转发
            boolean isForward = forwardService.isForwardFeedByUserId(feed.getId(), userId);
            vo.setIsForward(isForward);

            // 设置评论数
            long commentCount = commentService.getCommentCountByFeedId(feed.getId());
            vo.setCommentCount(commentCount);

            // 判断是否评论
            boolean isComment = commentService.isCommentFeedByUserId(feed.getId(), userId);
            vo.setIsComment(isComment);

            return vo;
        }).filter(obj->obj != null).collect(Collectors.toList());
    }

    public List<SimpleFeed> getForwardAndFeedBySelftUserId(Integer userId){
        return feedMapper.getForwardAndFeedBySelftUserId(userId);
    }
}
